package com.sam.security;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.config.ParentPackage;

import com.sam.core.BaseAction;
import com.sam.core.domain.Page;
import com.sam.security.domain.AsyncTreeNode;
import com.sam.security.domain.Permission;
import com.sam.security.domain.Role;
import com.sam.security.service.SecurityService;

/**
 * 系统权限资源管理
 * @project EMan
 * @author Sam
 * @version
 * @date Feb 10, 2009 9:45:18 AM
 */
@ParentPackage("default")
public class RoleAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	private SecurityService securityService;

	private Role role;

	private List<Role> list;

	private String menuId;

	private String roleId;

	public String addAllRolePermission() throws Exception {
		securityService.addAllRolePermission(roleId, menuId);
		sendJSON(true);
		return "json";
	}

	public String delete() throws Exception {
		securityService.deleteRole(dataString);
		sendJSON(true);
		return "json";
	}

	public String deleteAllRolePermission() throws Exception {
		securityService.deleteAllRolePermission(roleId, menuId);
		sendJSON(true);
		return "json";
	}

	@Override
	public String execute() throws Exception {
		List<Role> roles = new ArrayList<Role>();
		Role role = new Role();
		role.setId("1");
		role.setName("name");
		role.setDescription("/EMan/index.jsp");
		roles.add(role);
		sendJSON(roles.size(), roles);
		return "json";
	}

	public List<Role> getList() {
		return list;
	}

	public String getMenuId() {
		return menuId;
	}

	public Role getRole() {
		return role;
	}

	public String getRoleId() {
		return roleId;
	}

	public String load() throws Exception {
		Page<Role> page = new Page<Role>(start, limit);
		if (query != null && !query.equals("")) {
			securityService.searchRole(query, page);
			sendJSON(page.getTotalCount(), page.getResult());
		} else {
			securityService.loadRole(role, page);
			sendJSON(page.getTotalCount(), page.getResult());
		}

		return "json";
	}

	public String query() throws Exception {
		Page<Role> page = new Page<Role>(start, limit);
		List<Role> roles = null;
		if (query != null && !query.equals("")) {
			roles = securityService.searchRole(query, page);
		} else {
			roles = securityService.loadRole(role, page);
		}
		sendJSON(roles.size(), roles);
		return "json";
	}

	public String queryCheckedPermission() {
		List<Permission> per = securityService.queryCheckedPermission(roleId, menuId);
		sendJSON(per.size(), per);
		return "json";
	}

	public String queryUncheckedPermission() {
		List<Permission> per = securityService.queryUncheckedPermission(roleId, menuId);
		sendJSON(per.size(), per);
		return "json";
	}

	public String save() {
		if (dataString != null && !dataString.equals("")) {
			JSONArray jsonArr = JSONArray.fromObject(dataString);
			for (int i = 0; i < jsonArr.size(); i++) {
				JSONObject jsonObj = jsonArr.getJSONObject(i);
				Role res = (Role) JSONObject.toBean(jsonObj, Role.class);
				securityService.updateRole(res);
			}
		}
		sendJSON(true);
		return "json";
	}

	public String search() throws Exception {
		Page<Role> page = new Page<Role>(start, limit);
		List<Role> roles = null;
		if (query != null && !query.equals("")) {
			roles = securityService.searchRole(query, page);
		} else {
			roles = securityService.loadRole(role, page);
		}
		List<AsyncTreeNode> nodes = new ArrayList<AsyncTreeNode>();
		for (int i = 0; i < roles.size(); i++) {
			Role res = roles.get(i);
			AsyncTreeNode node = new AsyncTreeNode();
			node.setId(String.valueOf(res.getId()));
			node.setText(res.getName());
			node.setLeaf(true);
			nodes.add(node);
		}
		sendJSON(nodes);
		return "json";
	}

	public void setList(List<Role> list) {
		this.list = list;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public void setSecurityService(SecurityService securityService) {
		this.securityService = securityService;
	}

	public String update() throws Exception {
		securityService.updateRole(role);
		sendJSON(true);
		return "json";
	}

	public String updateRolePermission() throws Exception {
		String[] ids = dataString.split(",");
		securityService.updateRolePermission(roleId, menuId, ids);
		sendJSON(true);
		return "json";
	}

	public String updateRolePermissionAll() throws Exception {
		securityService.updateRolePermissionAll(roleId, menuId);
		sendJSON(true);
		return "json";
	}

}
